import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class CommodityService {
  serviceUrl = {
    commodityTypeList: '/goodstypes/list', // 商品分类列表
    commodityTypeMove: '/adzGoodsType/move', // 商品分类移动
    commodityTypeEdit: '/adzGoodsType/updateType', // 商品分类更新
    commodityTypeAdd: '/adzGoodsType/addType', // 商品分类添加
    commodityTypeDelete: '/adzGoodsType/deleteType', // 商品分类删除
    category: '/adzGoodsType/list', // 商品分类
    commodityList: '/adzGoods/list', // 商品列表
    commodityAdd: '/adzGoods/saveOrUpdateGoods', // 商品添加或更新
    commodityPutaway: '/adzGoods/upOrDownGoods', // 商品上架下架
    commodityDelete: '/adzGoods/delete', // 商品删除
    fileUpload: '/common/upload/file', // 文件上传

    textureListUrl: '/goodsTexture/list', // 手机壳材质列表
  };

  constructor(private httpClient: HttpClient) {
  }

  httpGet(url: string, params: any) {
    return this.httpClient.get(url, {params}).toPromise();
  }

  httpPost(url: string, body: any) {
    return this.httpClient.post(url, body).toPromise();
  }

  // 材质
  getTextureListData() {
    return this.httpClient.get(this.serviceUrl.textureListUrl, {}).toPromise();
  }

  /**
   * 文件上传
   * @param fileToUpload 
   * @returns 
   */
  uploadFileData(fileToUpload: File) {
    const formData: FormData = new FormData();
    formData.append('file', fileToUpload);
    formData.append('name', "watermark");
    return this.httpClient.post(this.serviceUrl.fileUpload, formData).toPromise();
  }
}
